{include file="public/header"}
<title>修改密码</title>
</head>
<body>
<div class="page-container">
	<form class="form form-horizontal" id="form" method="post">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>头像：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<div id="uploader-demo">
					<!--用来存放item-->
					<div id="fileList" class="uploader-list">
						<div id="<?php echo uniqid('logo')?>" class="file-item thumbnail">
							<div onclick="delParent(this)" style="cursor: pointer;background: url({$Think.DOMAIN_SRC}/static/h-ui/images/del.png) no-repeat;background-size:contain;height: 20px;width: 20px;position: absolute;z-index: 10;right: 5px;"></div>
							<img style="width: 100px;height: 100px;" src="{$admin.logo}">
							<input name="logo" type="hidden" value="{$admin.logo}">
							<div class="info"></div>
						</div>
					</div>
				</div>
				<div id="filePicker">选择图片</div>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text"  name="username" value="{$admin.username}">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>请输入原密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="password" class="input-text"  name="password">
			</div>
		</div>

		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">请输入新密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="password" class="input-text"  name="new_password">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">请确认新密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="password" class="input-text"  name="sure_password">
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<button class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 提交</button>
			</div>
		</div>
	</form>
</div>

{include file="public/footer"}
<script type="text/javascript">
    $(function(){
        $("#form").validate({
            rules:{
                "password":"required",
                "logo":"required",
                "username":"required"
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                $(form).ajaxSubmit({
                    "url":"{:Url('index/user/edit_password')}",
                    "success":function (res) {
                        console.log(res);
                        if(res.code==200){
                            var href="{:Url('index/login/login')}";
                            layer.closeAll();
                            function re() {
                                window.parent.location.href=href;
                            }
                            layer.msg(res.msg, {icon: 1, time: 2000}, function () {
                                setTimeout(re, 1);
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5,time:1000});
                        }
                    },
                    "error":function (data) {
                        layer.msg(data.responseText,{icon: 5,time:1000});
                    }
                });

            }
        });
    });
    function delParent(object){
        $(object).parent().remove();
        count-=1;
    }
    var count=0;
    $(function(){
        var $list = $("#fileList");
        var uploader = WebUploader.create({
            auto: true,
            // swf文件路径

            // 文件接收服务端。
            server: "{:Url('index/upload/upload_file')}",

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: '#filePicker',

            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            duplicate:true,

            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
        });

        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                '<div onclick="delParent(this)" style="cursor: pointer;background: url({$Think.DOMAIN_SRC}/static/h-ui/images/del.png) no-repeat;background-size:contain;height: 20px;width: 20px;position: absolute;z-index: 10;right: 5px;"></div>'+
                '<img style="width: 100px;height: 100px;">' +
                '<input name="logo" type="hidden" value="">'+
                '<div class="info">' + file.name + '</div>' +
                '</div>'
                ),
                $img = $li.find('img');


            // $list为容器jQuery实例
            $list.append( $li );

            // 创建缩略图
            // 如果为非图片文件，可以不用调用此方法。
            // thumbnailWidth x thumbnailHeight 为 100 x 100
            uploader.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }

                $img.attr( 'src', src );
            }, 100, 100 );
        });

        uploader.on( 'beforeFileQueued', function( file ) {
            if(count>0){
                return false;
            }
            return true;
        });

        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                $percent = $li.find('.progress span');

            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                    .appendTo( $li )
                    .find('span');
            }


            $percent.css( 'width', percentage * 100 + '%' );
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file,res ) {
            var data = JSON.parse(res._raw);
            if(data.errcode==300){
                layer.alert(data.msg);
            }
            var url = data.data;
            count+=1;
            $( '#'+file.id).find("logo").attr("src",url);
            $( '#'+file.id).find("input").val(url);
            $( '#'+file.id ).addClass('upload-state-done');
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                $error = $li.find('div.error');

            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }

            $error.text('上传失败');
        });

        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
        });
    });
</script>
</body>
</html>